<?php
class PaymentCardPartnerController extends CpController
{
    public function actionIndex()
    {
        $rate_card = ARate::getAllRateCard();
        
        /* Lấy tất cả loại thẻ trong dữ liệu */
        $providerCode = ACardPartner::getAllProviderCode();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 30;
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";        
        $search["provider_code"] = isset($_GET["provider_code"]) ? trim(strip_tags($_GET["provider_code"])) : "";
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["order"] = isset($_GET["order"]) ? trim(strip_tags($_GET["order"])) : "";        
        
        $card = array();        
        $numberPage = 0;
        $totalPartner = 0;
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
                           
            /* Lấy danh sách partner phát sinh doanh thu trong khoảng thời gian tìm kiếm */
            $partners = ACardPartner::GetPartner($search, $currentPage, $numberRecordPerPage);
            $totalPartner = ACardPartner::GetTotalPartner($search);
            $numberPage = ceil($totalPartner / $numberRecordPerPage);
            
            if(!empty($partners)){
                $search["list_partner_id"] = "";
                foreach($partners as $row){
                    $search["list_partner_id"] .= ", " . $row["partnerId"];
                }        
                $search["list_partner_id"] = ltrim($search["list_partner_id"], ", ");
                
                /* Lấy log sms của các đối tác này */
                $card = ACardPartner::getReport($search, $rate_card);
            }   
        }
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "providerCode"=>$providerCode
                , "card"=>$card
                , "totalPartner"=>$totalPartner
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage                
            )
        );
    }
    
    public function actionDetail()
    {
        $rate_card = ARate::getAllRateCard();
        
        /* Lấy tất cả loại thẻ trong dữ liệu */
        $providerCode = ACardPartner::getAllProviderCode();        
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 50;
        
        $search = array();
        $search["partnerId"] = isset($_GET["partnerId"]) ? intval($_GET["partnerId"]) : 0;
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "";
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : "";        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["provider_code"] = isset($_GET["provider_code"]) ? trim(strip_tags($_GET["provider_code"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["type"] = isset($_GET["type"]) ? trim(strip_tags($_GET["type"])) : "";
        
        $card = array();
        $totalCard = 0;        
        $numberPage = 0;
        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            
            /* Lấy log card của các thành viên này */
            $card = ACardPartner::getReportByPartner($search, $rate_card, $currentPage, $numberRecordPerPage);            
            $totalCard = ACardPartner::getTotalReportByPartner($search, $rate_card);            
            $numberPage = ceil($totalCard / $numberRecordPerPage);
        }
        
        $this->render(
            "detail"
            , array(
                "search"=>$search
                , "providerCode"=>$providerCode
                , "card"=>$card
                , "totalCard"=>$totalCard
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );   
    }
    
    public function actionExport()
    {
        $this->layout = false;
       
        $search = array();
        $search["partnerId"] = isset($_GET["partnerId"]) ? intval($_GET["partnerId"]) : 0;
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");                
        
       
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            /* Lấy log card của các đối tác này */
            $card = ACardPartner::getReportExport($search);                            
//            var_dump($card);die;
            $this->export($card, $search);
        }
    }
    
    private function export($card, $search)
    {
        Yii::import('application.vendors.PHPExcel',true);
        $objPHPExcel = new PHPExcel();            
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN
                ),
            ),
        ); 
        
        $objPHPExcel->setActiveSheetIndex(0);        
        $objPHPExcel->getActiveSheet()->setTitle('Đối soát');
        
        /* style mặc định */
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);        
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);        
        $objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);                            
        
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(7);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);        
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);        
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);                            
        
        /* gán dữ liệu */
        $objPHPExcel->getActiveSheet()->mergeCells('A1:H1');        
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, 'Biên bản đối soát');        
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray($styleArray);                        
        $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
        
        $objPHPExcel->getActiveSheet()->mergeCells('A2:H2');        
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 2, 'Nhà sản xuất');                    
        $objPHPExcel->getActiveSheet()->getStyle('A2:H2')->applyFromArray($styleArray);                                    
        
        $objPHPExcel->getActiveSheet()->mergeCells('A3:H3');        
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 3, 'Thời gian: Từ ngày ' . $search["toDate"] . " Đến ngày " .  $search["fromDate"]);                    
        $objPHPExcel->getActiveSheet()->getStyle('A3:H3')->applyFromArray($styleArray);                                    
        
        $objPHPExcel->getActiveSheet()->setCellValue("A5", 'STT');                    
        $objPHPExcel->getActiveSheet()->setCellValue("B5", 'Tên sản phẩm');                    
        $objPHPExcel->getActiveSheet()->setCellValue("C5", 'Mệnh giá');                    
        $objPHPExcel->getActiveSheet()->setCellValue("D5", 'Số thẻ');                            
        $objPHPExcel->getActiveSheet()->setCellValue("E5", 'DT sau telco');                            
        $objPHPExcel->getActiveSheet()->setCellValue("F5", 'DT mtop');                    
        $objPHPExcel->getActiveSheet()->setCellValue("G5", 'DT Wapmaster');                    
        $objPHPExcel->getActiveSheet()->setCellValue("H5", 'DT NSX');                                                    
        $objPHPExcel->getActiveSheet()->getStyle('A5:H5')->applyFromArray($styleArray);
        $objPHPExcel->getActiveSheet()->getStyle('A5:H5')->getFont()->setBold(true);   
        
        $i = 6;
        $j = 1;
        
        $total_card = 0;
        $total_telco = 0;        
        $total_mtop = 0;
        $total_wapmaster = 0;
        $total_partner = 0;
        
        foreach($card as $row){
            $objPHPExcel->getActiveSheet()->setCellValue("A" . $i, $j);
            $objPHPExcel->getActiveSheet()->setCellValue("B" . $i, $row["title"]);
            
            $t = 0;
            foreach($row as $price=>$value){
                if($price!="title"){
                    $total_card += $value["total_card"];
                    $total_telco += round($value["telco"]);                    
                    $total_mtop += round($value["mtop"]);
                    $total_wapmaster += round($value["wapmaster"]);
                    $total_partner += round($value["partner"]);
                    
                    $s = $i + $t;
                    $objPHPExcel->getActiveSheet()->getStyle('A' . $s . ':H' . $s)->applyFromArray($styleArray);
                    
                    $objPHPExcel->getActiveSheet()->setCellValue("C" . $s, $price);
                    $objPHPExcel->getActiveSheet()->setCellValueExplicit("D" . $s, Common::format_price($value["total_card"]), PHPExcel_Cell_DataType::TYPE_STRING);
                    
                    $objPHPExcel->getActiveSheet()->setCellValueExplicit("E" . $s, Common::format_price(round($value["telco"])), PHPExcel_Cell_DataType::TYPE_STRING);
                    $objPHPExcel->getActiveSheet()->getStyle("E" . $s)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    
                    $objPHPExcel->getActiveSheet()->setCellValueExplicit("F" . $s, Common::format_price(round($value["mtop"])), PHPExcel_Cell_DataType::TYPE_STRING);
                    $objPHPExcel->getActiveSheet()->getStyle("F" . $s)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    
                    $objPHPExcel->getActiveSheet()->setCellValueExplicit("G" . $s, Common::format_price(round($value["wapmaster"])), PHPExcel_Cell_DataType::TYPE_STRING);
                    $objPHPExcel->getActiveSheet()->getStyle("G" . $s)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    
                    $objPHPExcel->getActiveSheet()->setCellValueExplicit("H" . $s, Common::format_price(round($value["partner"])), PHPExcel_Cell_DataType::TYPE_STRING);                    
                    $objPHPExcel->getActiveSheet()->getStyle("H" . $s)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    $t++;    
                }                
            }

            $k = $i + $t - 1;
            $objPHPExcel->getActiveSheet()->mergeCells('A' . $i . ':A' . $k);
            $objPHPExcel->getActiveSheet()->mergeCells('B' . $i . ':B' . $k);
            
            $i = $k + 1;
            $j++;
        }
        
        $objPHPExcel->getActiveSheet()->mergeCells('A' . $i . ':C' . ($i + 1));        
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i, 'TỔNG');                    
        $objPHPExcel->getActiveSheet()->getStyle('A' . $i . ':H' . ($i + 1))->applyFromArray($styleArray);                        
        $objPHPExcel->getActiveSheet()->getStyle('A' . $i . ':H' . ($i + 1))->getFont()->setBold(true);      

        
        $objPHPExcel->getActiveSheet()->setCellValueExplicit("D" . $i, Common::format_price($total_card), PHPExcel_Cell_DataType::TYPE_STRING);        
        $objPHPExcel->getActiveSheet()->mergeCells('D' . $i . ':D' . ($i + 1));        
        
        $objPHPExcel->getActiveSheet()->setCellValueExplicit("E" . $i, Common::format_price($total_telco), PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);            
        $objPHPExcel->getActiveSheet()->mergeCells('E' . $i . ':E' . ($i + 1));        
        
        $objPHPExcel->getActiveSheet()->setCellValueExplicit("F" . $i, Common::format_price($total_mtop), PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);            
        $objPHPExcel->getActiveSheet()->mergeCells('F' . $i . ':F' . ($i + 1));
        
        $objPHPExcel->getActiveSheet()->setCellValueExplicit("G" . $i, Common::format_price($total_wapmaster), PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);            
        $objPHPExcel->getActiveSheet()->mergeCells('G' . $i . ':G' . ($i + 1));
        
        $objPHPExcel->getActiveSheet()->setCellValueExplicit("H" . $i, Common::format_price($total_partner), PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);            
        $objPHPExcel->getActiveSheet()->mergeCells('H' . $i . ':H' . ($i + 1));        
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                    
        $curr_dir = getcwd();         
        $curr_dir .= "/upload/report/" ;            
        $filename = "doi-soat-partner-card-" . date("Y-m-d_H_i_s", time()) . ".xlsx";
        $pathname = $curr_dir . $filename;
        $objWriter->save($pathname);
                 
        Common::download($filename, $filename, "report/");
        
    }
}  
?>